function addCookie(name, value) { 
	var expireDate = new Date()
	expireDate.setDate(expireDate.getDate() + 90)
	document.cookie = escape(name) + '=' + escape(value) +';expires=' + expireDate.toGMTString() + ';path=/;' 
}

// 是否有token
const hasToken = () => {
	let token = document.cookie.split(';').find(res => /^token\=/.test(res.trim()))
	let mobile = document.cookie.split(';').find(res => /^mobile\=/.test(res.trim()))

	if (token && mobile) {
		return true
	}
	return false
}

// 存本地的token
const setToken = (token, mobile) => {
	return new Promise((resolve, reject) => {
		addCookie('token', token)
		addCookie('mobile', mobile)
		resolve()
	})
}

// 获取电话号码
const getMobile = () => {
	// return '13764001198'
	let mobile = document.cookie.split(';').find(res => /^mobile\=/.test(res.trim()))
	if (mobile) {
		mobile = mobile.trim()
		return mobile.split('=')[1]
	}
	return undefined
}

// 清除token
const clearToken = () => {
	return new Promise((resolve, reject) => {
		var exp = new Date().toGMTString()
		document.cookie = `token=-1;expires=${exp};path=/;`
		document.cookie = `mobile=-1;expires=${exp};path=/;`
		resolve()
	})
}

// 导出
export {
	setToken,
	clearToken,
	hasToken,
	getMobile,
}